package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.AdminMapper;
import com.atguigu.ssyx.acl.mapper.AdminRoleMapper;
import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {

        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<Role>();
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName, roleName);
        }
        Page<Role> rolePage = baseMapper.selectPage(pageParam, wrapper);

        return rolePage;
    }

    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {

        // 1、根据adminId查询AdminRole表中的所有的roleId
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        List<Long> RoleIdList = adminRoleList.stream().map(AdminRole::getRoleId).collect(Collectors.toList());

        // 查询所有的role
        List<Role> allRolesList = roleMapper.selectList(null);

//        List<Role> assignRoleList = new ArrayList<>();
//        for(Role role : allRolesList){
//            if(RoleIdList.contains(role.getId())){
//                assignRoleList.add(role);
//            }
//        }

        List<Role> assignRoleList = allRolesList.stream().filter(m -> RoleIdList.contains(m.getId())).collect(Collectors.toList());

        HashMap<String, Object> result = new HashMap<>();
        //所有角色列表
        result.put("allRolesList",allRolesList);
        //用户分配角色列表
        result.put("assignRoles",assignRoleList);

        return result;
    }

    @Override
    public void saveAdminRole(Long adminId, Long[] roleId) {
        //1、删除之前的所有的
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleService.remove(wrapper);


        List<AdminRole> adminRoles = new ArrayList<>();

        //2、批量保存
        for (Long x : roleId) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(x);
            adminRoles.add(adminRole);
        }

        adminRoleService.saveBatch(adminRoles);
    }

}
